﻿namespace Hidistro.SqlDal.Store
{
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Core.Enums;
    using Hidistro.Entities;
    using Hidistro.Entities.Comments;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Globalization;
    using System.Text;

    public class MessageDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public int DeleteMemberMessages(IList<long> messageList)
        {
            string str = string.Empty;
            foreach (long num in messageList)
            {
                if (string.IsNullOrEmpty(str))
                {
                    str = str + num.ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    str = str + "," + num.ToString(CultureInfo.InvariantCulture);
                }
            }
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(string.Format("delete from Hishop_MemberMessageBox where MessageId in ({0}) ", str));
            return this.database.ExecuteNonQuery(sqlStringCommand);
        }

        public MessageBoxInfo GetMemberMessage(long messageId)
        {
            MessageBoxInfo info = null;
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT * FROM vw_Hishop_MemberMessageBox WHERE MessageId=@MessageId;");
            this.database.AddInParameter(sqlStringCommand, "MessageId", DbType.Int64, messageId);
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                if (reader.Read())
                {
                    info = DataMapper.PopulateMessageBox(reader);
                }
            }
            return info;
        }

        public DbQueryResult GetMemberReceivedMessages(MessageBoxQuery query)
        {
            string filter = string.Format("Accepter='{0}'", query.Accepter);
            return DataHelper.PagingByRownumber(query.PageIndex, query.PageSize, "MessageId", SortAction.Desc, query.IsCount, "vw_Hishop_MemberMessageBox", "MessageId", filter, "*");
        }

        public DbQueryResult GetMemberSendedMessages(MessageBoxQuery query)
        {
            string filter = string.Format("Sernder='{0}'", query.Sernder);
            return DataHelper.PagingByRownumber(query.PageIndex, query.PageSize, "MessageId", SortAction.Desc, query.IsCount, "vw_Hishop_MemberMessageBox", "MessageId", filter, "*");
        }

        public bool InsertMessage(MessageBoxInfo messageBoxInfo)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("BEGIN TRAN ");
            builder.Append("DECLARE @ContentId int ");
            builder.Append("DECLARE @errorSun INT ");
            builder.Append("SET @errorSun=0 ");
            builder.Append("INSERT INTO [Hishop_MessageContent]([Title],[Content],[Date]) ");
            builder.Append("VALUES(@Title,@Content,@Date) ");
            builder.Append("SET @ContentId = @@IDENTITY  ");
            builder.Append("SET @errorSun=@errorSun+@@ERROR  ");
            builder.Append("INSERT INTO [Hishop_MemberMessageBox]([ContentId],[Sernder],[Accepter],[IsRead]) ");
            builder.Append("VALUES(@ContentId,@Sernder ,@Accepter,@IsRead) ");
            builder.Append("SET @errorSun=@errorSun+@@ERROR  ");
            builder.Append("INSERT INTO [Hishop_ManagerMessageBox]([ContentId],[Sernder],[Accepter],[IsRead]) ");
            builder.Append("VALUES(@ContentId,@Sernder ,@Accepter,@IsRead) ");
            builder.Append("SET @errorSun=@errorSun+@@ERROR  ");
            builder.Append("IF @errorSun<>0 ");
            builder.Append("BEGIN ");
            builder.Append("ROLLBACK TRANSACTION  ");
            builder.Append("END ");
            builder.Append("ELSE ");
            builder.Append("BEGIN ");
            builder.Append("COMMIT TRANSACTION  ");
            builder.Append("END ");
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(builder.ToString());
            this.database.AddInParameter(sqlStringCommand, "Title", DbType.String, messageBoxInfo.Title);
            this.database.AddInParameter(sqlStringCommand, "Content", DbType.String, messageBoxInfo.Content);
            this.database.AddInParameter(sqlStringCommand, "Date", DbType.DateTime, DataHelper.GetSafeDateTimeFormat(DateTime.Now));
            this.database.AddInParameter(sqlStringCommand, "Sernder", DbType.String, messageBoxInfo.Sernder);
            this.database.AddInParameter(sqlStringCommand, "Accepter", DbType.String, messageBoxInfo.Accepter);
            this.database.AddInParameter(sqlStringCommand, "IsRead", DbType.Boolean, messageBoxInfo.IsRead);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool PostMemberMessageIsRead(long messageId)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("Update Hishop_MemberMessageBox set IsRead=1 where MessageId=@MessageId");
            this.database.AddInParameter(sqlStringCommand, "MessageId", DbType.Int64, messageId);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool SendMessage(string subject, string message, string sendto)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("DECLARE @ContentId int; INSERT INTO [Hishop_MessageContent]([Title],[Content],[Date]) VALUES (@Title,@Content,@Date) SET @ContentId = @@IDENTITY INSERT INTO [Hishop_MemberMessageBox]([ContentId],[Sernder],[Accepter],[IsRead]) VALUES (@ContentId,'admin' ,@Accepter,0)");
            this.database.AddInParameter(sqlStringCommand, "Title", DbType.String, subject);
            this.database.AddInParameter(sqlStringCommand, "Content", DbType.String, message);
            this.database.AddInParameter(sqlStringCommand, "Date", DbType.DateTime, DateTime.Now);
            this.database.AddInParameter(sqlStringCommand, "Accepter", DbType.String, sendto);
            return (this.database.ExecuteNonQuery(sqlStringCommand) >= 1);
        }
    }
}

